iT邦幫忙

2024 iThome 鐵人賽

DAY 6
0

今天來解YKL06(UVA10190):Divide, But Not Quite Conquer

Divide, But Not Quite Conquer

https://ithelp.ithome.com.tw/upload/images/20240920/20155574KO5vog61VQ.png

https://ithelp.ithome.com.tw/upload/images/20240920/20155574V4dY79UGoG.png

n / m 除到最後要是int也要是1
如果不是的話,output Boring!
m不可能大於n

#include <iostream>
#include <vector>
using namespace std;

int main(){
	int n,m;
	while(cin >> n >> m){
		if(m <= 1 || n < m){
			cout << "Boring!" << endl;
			continue;
		}
		
		vector<int> sequence;
		sequence.push_back(n);
		bool isBoring = false;
		while(n > 1){
			if(n % m != 0){
				isBoring = true;
				break;
			}
			n = n / m;
			sequence.push_back(n);
		}
		if(n!=1){
			isBoring = true;
		}
		
		if(isBoring){
			cout << "Boring!" << endl;
		}else{
			cout << sequence[0];
			for(int i = 1;i < sequence.size();i++){
				if(i > 0){
					cout << " " << sequence[i];
				}
			}
			cout << endl;
		}
	}
	return 0;
}

上一篇
CPE C++ 刷題 Day 5
下一篇
CPE C++ 刷題 Day 7
系列文
CPE C++ 刷題20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言